{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果本地没有安装工具包，请去掉“#”，再执行\n",
    "# !pip install -i https://mirrors.aliyun.com/pypi/simple/ xlwt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>读取Excel的内容</font>**<br/>\n",
    "<img src=\"images/Excel工资单明细.png\" align=\"left\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>新建Excel的内容（合并单元格）</font>**<br/>\n",
    "**<font color=red>为每个员工创建一个工作表，工作表名是员工姓名</font>**<br/>\n",
    "<img src=\"images/Excel工资单_02.png\" align=\"left\"></img>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取Excel，获取字段名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入Excel读写工具包\n",
    "import xlrd\n",
    "import xlwt\n",
    "\n",
    "# 从（from）本地 xlwt_styles.py（.py扩展名省略）文件中，导入（import）创建样式的函数（create_style）\n",
    "from xlwt_styles import create_style\n",
    "\n",
    "# 定义两个变量（xls_filename_rd、xls_filename_wt）\n",
    "xls_filename_rd = \"./files/Excel_工资单明细.xls\" # 读取的 Excel 文件名\n",
    "xls_filename_wt = \"./files/Excel_员工工资单_02.xls\" # 新建的 Excel 文件名\n",
    "# *******************************\n",
    "# 虽然变量命名只要符合命名规则即可，但建议变量命名最好要有意义，对读懂代码很有帮助\n",
    "# xls:Excel扩展名  filename:文件名  rd:read读取  wt:write写入\n",
    "# *******************************\n",
    "\n",
    "# 读取 Excel（xls_filename_rd 变量刚才已经定义），得到工作簿，赋值给变量 workbook_rd\n",
    "workbook_rd = xlrd.open_workbook(xls_filename_rd)\n",
    "\n",
    "# 获取第一个工作表，赋值给变量 sheet_rd\n",
    "sheet_rd = workbook_rd.sheet_by_index(0)\n",
    "\n",
    "# 获取工作表中表格的所有字段（第一行是字段名），记住后面会用到\n",
    "field_names = sheet_rd.row_values(0)\n",
    "print(\"字段名：\",field_names)  # 打印一下看看字段名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取Excel，获取员工姓名和明细数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建两个“空”列表变量\n",
    "staff_names = [] # 员工姓名列表\n",
    "staff_values_list = [] # 员工数据明细列表（这个就是列表中的列表）\n",
    "\n",
    "# 因为第一行（索引=0）是字段名，所以从第二行（索引=1）开始读数据\n",
    "for row_index in range(1, sheet_rd.nrows):\n",
    "    values = sheet_rd.row_values(row_index)  # 获取员工的数据列表\n",
    "    # print(values)           # 可以删除注释，打印这一行数据列表看看\n",
    "    staff_name = values[2]    # 第三列是姓名\n",
    "    # print(staff_name)       # 可以删除注释，打印一下看看\n",
    "    staff_names.append(staff_name)  # append\n",
    "    staff_values_list.append(values) # 列表中可以再包含列表 [[...],[...]]\n",
    "\n",
    "# 打印读取的员工姓名列表、员工数据明细列表\n",
    "# print(\"员工姓名列表：\",staff_names)           # 可以删除注释，打印一下看看\n",
    "# print(\"员工数据明细列表：\",staff_values_list) # 可以删除注释，打印一下看看"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 新建 Excel，创建样式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建写入工作簿对象\n",
    "workbook_wt = xlwt.Workbook(encoding='utf-8')\n",
    "\n",
    "# 看看要生成的表格样式，共有4个\n",
    "# 合并单元格的样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=居中）\n",
    "merge_style = create_style(back_color=57, font_color=1, font_size=12, horz=2)  # 标题显示用黄色背景图案\n",
    "# 字段样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=靠右）\n",
    "field_style = create_style(back_color=57, font_color=1, font_size=12, horz=3)\n",
    "# 数据值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=居中）\n",
    "value_style = create_style(back_color=1, font_color=0, font_size=12, horz=2)\n",
    "# 金额值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=靠右，人民币格式）\n",
    "rmb_style = create_style(back_color=1, font_color=0, font_size=12, horz=3, num_format_str=\"￥#,##0.00;￥-#,##0.00\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>Excel颜色索引对照表</font>**<br/>\n",
    "<img src=\"images/Excel颜色对照表.png\" align=\"left\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语法：条件判断"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果条件成立，则我要运行代码A，如果条件不成立，我就运行代码B\n",
    "```python\n",
    "if 条件?:\n",
    "   run(A)\n",
    "else:\n",
    "   run(B)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "# 每次生成的结果不一样（1 到 10）\n",
    "num = random.randint(1,10)\n",
    "print(\"随机 num =\", num)\n",
    "if num >= 5:\n",
    "    print(\"num 大于等于 5\")\n",
    "else:\n",
    "    print(\"num 小于 5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看条件\n",
    "print(num >= 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 布尔类型（Boolean）：\n",
    "# 布尔类型只有两个值：True、False，对应中文为：“是 、否”或者“真、假”"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 两个循环写入数据 ，保存 Excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一循环：遍历每个员工的姓名，每个姓名创建一个工作表\n",
    "for staff_index, staff_name in enumerate(staff_names):\n",
    "    # 创建工作表，工作表名就是员工姓名\n",
    "    sheet_wt_staff = workbook_wt.add_sheet(staff_name)\n",
    "\n",
    "    # 获取员工的数据明细\n",
    "    staff_data = staff_values_list[staff_index]\n",
    "    # 第二循环：遍历字段和数据\n",
    "    for field_index, field_name in enumerate(field_names):\n",
    "        row = field_index + 1 # field_index 从 0 开始，则 row 从 1 开始\n",
    "\n",
    "        if field_index < 14: # “实发”是最后一个字段名（索引=14），不写入，后面合并单元格再写入\n",
    "            # 第3列（c=2）写入字段名\n",
    "            sheet_wt_staff.write(r=row, c=2, label=field_name, style=field_style)\n",
    "        # else: 如果没有逻辑，可以省略。只对于“实发”字段名不写入了，后面合并单元格再写入\n",
    "\n",
    "        value = staff_data[field_index]\n",
    "\n",
    "        if field_index <= 2:   # 0，1，2 三个字段分别是月份、部门、姓名\n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=value_style) # 第4列（c=3）写入数据\n",
    "        else:    # 3、4、5...都是金额字段，用RMB货币样式            \n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=rmb_style) # 第4列（c=3）写入数据\n",
    "\n",
    "    # 设置字段列和数据列的列宽 256 × 宽度默认单位（字符）\n",
    "    sheet_wt_staff.col(1).width = 256 * 12\n",
    "    sheet_wt_staff.col(2).width = 256 * 15\n",
    "    sheet_wt_staff.col(3).width = 256 * 20\n",
    "    # 合并单元格写入\n",
    "    sheet_wt_staff.write_merge(r1=1, r2=8, c1=1, c2=1, label=\"收\\n入\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=9, r2=14, c1=1, c2=1, label=\"扣\\n款\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=15, r2=15, c1=1, c2=2, label=\"实发\", style=field_style)\n",
    "\n",
    "workbook_wt.save(xls_filename_wt) # 保存 Excel\n",
    "\n",
    "print(\"文件 %s 生成成功\" % xls_filename_wt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue size=4> 条件判断语法 if 的用法</font>**\n",
    "\n",
    "<font color=blue size=4> 1、两个分支</font>\n",
    "<pre>\n",
    "if 条件 ?:\n",
    "   run(A)\n",
    "else:\n",
    "   run(B)\n",
    "</pre>\n",
    "<hr/>\n",
    "\n",
    "<font color=blue size=4> 2、三个分支（多分支）</font>\n",
    "<pre>\n",
    "if 条件1 ?:\n",
    "   run(A)\n",
    "elif 条件2 ?:\n",
    "   run(B)\n",
    "else:\n",
    "   run(C)\n",
    "</pre>\n",
    "\n",
    "<font color=blue size=4> 3、布尔类型（Boolean）</font>\n",
    "\n",
    "<font color=blue size=4> 只有两个值：True（是、真）、False（否、假）</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 完整代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入Excel读写工具包\n",
    "import xlrd\n",
    "import xlwt\n",
    "\n",
    "# 从（from）本地 xlwt_styles.py（.py扩展名省略）文件中，导入（import）创建样式的函数（create_style）\n",
    "from xlwt_styles import create_style\n",
    "\n",
    "# 定义两个变量（xls_filename_rd、xls_filename_wt）\n",
    "xls_filename_rd = \"./files/Excel_工资单明细.xls\" # 读取的 Excel 文件名\n",
    "xls_filename_wt = \"./files/Excel_员工工资单_02.xls\" # 新建的 Excel 文件名\n",
    "# *******************************\n",
    "# 虽然变量命名只要符合命名规则即可，但建议变量命名最好要有意义，对读懂代码很有帮助\n",
    "# xls:Excel扩展名  filename:文件名  rd:read读取  wt:write写入\n",
    "# *******************************\n",
    "\n",
    "# 读取 Excel（xls_filename_rd 变量刚才已经定义），得到工作簿，赋值给变量 workbook_rd\n",
    "workbook_rd = xlrd.open_workbook(xls_filename_rd)\n",
    "\n",
    "# 获取第一个工作表，赋值给变量 sheet_rd\n",
    "sheet_rd = workbook_rd.sheet_by_index(0)\n",
    "\n",
    "# 获取工作表中表格的所有字段（第一行是字段名），记住后面会用到\n",
    "field_names = sheet_rd.row_values(0)\n",
    "print(\"字段名：\",field_names)  # 打印一下看看字段名\n",
    "\n",
    "# 创建两个“空”列表变量\n",
    "staff_names = [] # 员工姓名列表\n",
    "staff_values_list = [] # 员工数据明细列表（这个就是列表中的列表）\n",
    "\n",
    "# 因为第一行（索引=0）是字段名，所以从第二行（索引=1）开始读数据\n",
    "for row_index in range(1, sheet_rd.nrows):\n",
    "    values = sheet_rd.row_values(row_index)  # 获取员工的数据列表\n",
    "    # print(values)           # 可以删除注释，打印这一行数据列表看看\n",
    "    staff_name = values[2]    # 第三列是姓名\n",
    "    # print(staff_name)       # 可以删除注释，打印一下看看\n",
    "    staff_names.append(staff_name)  # append\n",
    "    staff_values_list.append(values) # 列表中可以再包含列表 [[...],[...]]\n",
    "\n",
    "# 打印读取的员工姓名列表、员工数据明细列表\n",
    "# print(\"员工姓名列表：\",staff_names)           # 可以删除注释，打印一下看看\n",
    "# print(\"员工数据明细列表：\",staff_values_list) # 可以删除注释，打印一下看看\n",
    "\n",
    "# 创建写入工作簿对象\n",
    "workbook_wt = xlwt.Workbook(encoding='utf-8')\n",
    "\n",
    "# 看看要生成的表格样式，共有4个\n",
    "# 合并单元格的样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=居中）\n",
    "merge_style = create_style(back_color=57, font_color=1, font_size=12, horz=2)  # 标题显示用黄色背景图案\n",
    "# 字段样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=靠右）\n",
    "field_style = create_style(back_color=57, font_color=1, font_size=12, horz=3)\n",
    "# 数据值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=居中）\n",
    "value_style = create_style(back_color=1, font_color=0, font_size=12, horz=2)\n",
    "# 金额值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=靠右，人民币格式）\n",
    "rmb_style = create_style(back_color=1, font_color=0, font_size=12, horz=3, num_format_str=\"￥#,##0.00;￥-#,##0.00\")\n",
    "\n",
    "# 第一循环：遍历每个员工的姓名，每个姓名创建一个工作表\n",
    "for staff_index, staff_name in enumerate(staff_names):\n",
    "    # 创建工作表，工作表名就是员工姓名\n",
    "    sheet_wt_staff = workbook_wt.add_sheet(staff_name)\n",
    "\n",
    "    # 获取员工的数据明细\n",
    "    staff_data = staff_values_list[staff_index]\n",
    "    # 第二循环：遍历字段和数据\n",
    "    for field_index, field_name in enumerate(field_names):\n",
    "        row = field_index + 1 # field_index 从 0 开始，则 row 从 1 开始\n",
    "\n",
    "        if field_index < 14: # “实发”是最后一个字段名（索引=14），不写入，后面合并单元格再写入\n",
    "            # 第3列（c=2）写入字段名\n",
    "            sheet_wt_staff.write(r=row, c=2, label=field_name, style=field_style)\n",
    "        # else: 如果没有逻辑，可以省略。只对于“实发”字段名不写入了，后面合并单元格再写入\n",
    "\n",
    "        value = staff_data[field_index]\n",
    "\n",
    "        if field_index <= 2:   # 0，1，2 三个字段分别是月份、部门、姓名\n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=value_style) # 第4列（c=3）写入数据\n",
    "        else:    # 3、4、5...都是金额字段，用RMB货币样式            \n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=rmb_style) # 第4列（c=3）写入数据\n",
    "\n",
    "    # 设置字段列和数据列的列宽 256 × 宽度默认单位（字符）\n",
    "    sheet_wt_staff.col(1).width = 256 * 12\n",
    "    sheet_wt_staff.col(2).width = 256 * 15\n",
    "    sheet_wt_staff.col(3).width = 256 * 20\n",
    "    # 合并单元格写入\n",
    "    sheet_wt_staff.write_merge(r1=1, r2=8, c1=1, c2=1, label=\"收\\n入\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=9, r2=14, c1=1, c2=1, label=\"扣\\n款\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=15, r2=15, c1=1, c2=2, label=\"实发\", style=field_style)\n",
    "\n",
    "workbook_wt.save(xls_filename_wt) # 保存 Excel\n",
    "\n",
    "print(\"文件 %s 生成成功\" % xls_filename_wt)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "**<font color=blue size=4>上面的课程是生成一个工作簿(Workbook)，多个工作表（工作表名称为员工姓名）<br/>\n",
    "    现在的要求是：一个员工生成一个工作簿(Workbook)，工作簿命名包含员工姓名，如下图：Excel_陈浩的工资单.xls\"</font>**\n",
    "\n",
    "<font color=red size=4>这个思考题对于初学者可能会有点困难，代码虽然改起来非常简单，就是看怎么改，希望大家尝试一下改上面的代码</font>\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<font color=blue>新建Excel的内容（合并单元格）</font>**<br/>\n",
    "**<font color=red>为每个员工创建一个工作簿文件，命名包含员工姓名</font>**<br/>\n",
    "<img src=\"images/Excel工资单_03.png\" align=\"left\"></img>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 请在下面给出思考题的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入Excel读写工具包\n",
    "import xlrd\n",
    "import xlwt\n",
    "\n",
    "# 从（from）本地 xlwt_styles.py（.py扩展名省略）文件中，导入（import）创建样式的函数（create_style）\n",
    "from xlwt_styles import create_style\n",
    "\n",
    "# 读取 Excel（xls_filename_rd 变量刚才已经定义），得到工作簿，赋值给变量 workbook_rd\n",
    "workbook_rd = xlrd.open_workbook(xls_filename_rd)\n",
    "\n",
    "# 获取第一个工作表，赋值给变量 sheet_rd\n",
    "sheet_rd = workbook_rd.sheet_by_index(0)\n",
    "\n",
    "# 获取工作表中表格的所有字段（第一行是字段名），记住后面会用到\n",
    "field_names = sheet_rd.row_values(0)\n",
    "print(\"字段名：\",field_names)  # 打印一下看看字段名\n",
    "\n",
    "# TODO:请在这里自己实现，可以从上面学过的代码中复制粘贴，不要从下面思考题答案代码复制粘贴\n",
    "....."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题正确答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入Excel读写工具包\n",
    "import xlrd\n",
    "import xlwt\n",
    "\n",
    "# 从（from）本地 xlwt_styles.py（.py扩展名省略）文件中，导入（import）创建样式的函数（create_style）\n",
    "from xlwt_styles import create_style\n",
    "\n",
    "xls_filename_rd = \"./files/Excel_工资单明细.xls\" # 读取的 Excel 文件名\n",
    "\n",
    "\n",
    "# 读取 Excel（xls_filename_rd 变量刚才已经定义），得到工作簿，赋值给变量 workbook_rd\n",
    "workbook_rd = xlrd.open_workbook(xls_filename_rd)\n",
    "\n",
    "# 获取第一个工作表，赋值给变量 sheet_rd\n",
    "sheet_rd = workbook_rd.sheet_by_index(0)\n",
    "\n",
    "# 获取工作表中表格的所有字段（第一行是字段名），记住后面会用到\n",
    "field_names = sheet_rd.row_values(0)\n",
    "# print(\"字段名：\",field_names)  # 打印一下看看字段名\n",
    "\n",
    "# 创建两个“空”列表变量\n",
    "staff_names = [] # 员工姓名列表\n",
    "staff_values_list = [] # 员工数据明细列表（这个就是列表中的列表）\n",
    "\n",
    "# 因为第一行（索引=0）是字段名，所以从第二行（索引=1）开始读数据\n",
    "for row_index in range(1, sheet_rd.nrows):\n",
    "    values = sheet_rd.row_values(row_index)  # 获取员工的数据列表\n",
    "    staff_name = values[2]    # 第三列是姓名\n",
    "    staff_names.append(staff_name)  # append\n",
    "    staff_values_list.append(values) # 列表中可以再包含列表 [[...],[...]]\n",
    "\n",
    "# *******************************************************************\n",
    "# 注意：以下代码不用了，注释掉了，知道原因吗？\n",
    "# workbook_wt = xlwt.Workbook(encoding='utf-8')\n",
    "\n",
    "# 看看要生成的表格样式，共有4个\n",
    "# 合并单元格的样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=居中）\n",
    "merge_style = create_style(back_color=57, font_color=1, font_size=12, horz=2)  # 标题显示用黄色背景图案\n",
    "# 字段样式（背景=草绿，文字颜色=白色，字号=12，水平对齐=靠右）\n",
    "field_style = create_style(back_color=57, font_color=1, font_size=12, horz=3)\n",
    "# 数据值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=居中）\n",
    "value_style = create_style(back_color=1, font_color=0, font_size=12, horz=2)\n",
    "# 金额值的样式（背景=白色，文字颜色=黑色，字号=12，水平对齐=靠右，人民币格式）\n",
    "rmb_style = create_style(back_color=1, font_color=0, font_size=12, horz=3, num_format_str=\"￥#,##0.00;￥-#,##0.00\")\n",
    "\n",
    "# 第一循环：遍历每个员工的姓名，每个员工创建一个工作簿文件\n",
    "for staff_index, staff_name in enumerate(staff_names):\n",
    "    # *******************************************************************\n",
    "    # 创建写入工作簿对象（放到循环内了，因为一个循环需要新建一个Excel文件）\n",
    "    # *******************************************************************\n",
    "    workbook_wt = xlwt.Workbook(encoding='utf-8')\n",
    "    \n",
    "    # 创建工作表，工作表名就是员工姓名\n",
    "    sheet_wt_staff = workbook_wt.add_sheet(staff_name)\n",
    "\n",
    "    # 获取员工的数据明细\n",
    "    staff_data = staff_values_list[staff_index]\n",
    "    # 第二循环：遍历字段和数据\n",
    "    for field_index, field_name in enumerate(field_names):\n",
    "        row = field_index + 1 # field_index 从 0 开始，则 row 从 1 开始\n",
    "\n",
    "        if field_index < 14: # “实发”是最后一个字段名（索引=14），不写入，后面合并单元格再写入\n",
    "            # 第3列（c=2）写入字段名\n",
    "            sheet_wt_staff.write(r=row, c=2, label=field_name, style=field_style)\n",
    "        # else: 如果没有逻辑，可以省略。只对于“实发”字段名不写入了，后面合并单元格再写入\n",
    "\n",
    "        value = staff_data[field_index]\n",
    "\n",
    "        if field_index <= 2:   # 0，1，2 三个字段分别是月份、部门、姓名\n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=value_style) # 第4列（c=3）写入数据\n",
    "        else:    # 3、4、5...都是金额字段，用RMB货币样式            \n",
    "            sheet_wt_staff.write(r=row, c=3, label=value, style=rmb_style) # 第4列（c=3）写入数据\n",
    "\n",
    "    # 设置字段列和数据列的列宽 256 × 宽度默认单位（字符）\n",
    "    sheet_wt_staff.col(1).width = 256 * 12\n",
    "    sheet_wt_staff.col(2).width = 256 * 15\n",
    "    sheet_wt_staff.col(3).width = 256 * 20\n",
    "    # 合并单元格写入\n",
    "    sheet_wt_staff.write_merge(r1=1, r2=8, c1=1, c2=1, label=\"收\\n入\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=9, r2=14, c1=1, c2=1, label=\"扣\\n款\", style=merge_style)\n",
    "    sheet_wt_staff.write_merge(r1=15, r2=15, c1=1, c2=2, label=\"实发\", style=field_style)\n",
    "    # *******************************************************************\n",
    "    # 一个循环需要新建一个Excel文件，文件名变化的部分就是员工姓名\n",
    "    xls_filename_wt = \"./files/Excel_%s的工资单.xls\" % staff_name    \n",
    "    workbook_wt.save(xls_filename_wt) # 保存 Excel\n",
    "    print(\"文件 %s 生成成功\" % xls_filename_wt)\n",
    "    # *******************************************************************"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
